import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../../const.dart';
import '../component/chat_input.dart';
import '../component/message.dart';
import '../controllers/message_controller.dart';

class MessageView extends GetView<MessageController> {
  AppBar buildAppBar() {
    return AppBar(
      automaticallyImplyLeading: false,
      title: Row(
        children: [
           BackButton(),
           GestureDetector(
             onTap: () {
               this.controller.toProfile1();
             },
             child: controller.friendUrl == ""? Icon(Icons.account_circle_rounded, size: 45,): CircleAvatar(
              backgroundImage: NetworkImage(controller.friendUrl),
          ),
           ),
          const SizedBox(width: kDefaultPadding * 0.75),
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children:  [
              Text(controller.friendName, style: TextStyle(fontSize: 16)),
              // Text('Active 3m ago', style: TextStyle(fontSize: 12))
            ],
          )
        ],
      ),
      // actions: [
      //   IconButton(onPressed: () {}, icon: const Icon(Icons.call)),
      //   IconButton(onPressed: () {}, icon: const Icon(Icons.videocam)),
      //   const SizedBox(
      //     width: kDefaultPadding / 2,
      //   )
      // ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: buildAppBar(),
      body: Obx(
              () => Column(
            children: [
              Expanded(
                  child: Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: kDefaultPadding),
                child: ListView.builder(
                  itemCount: controller
                              .messageService.messageMap[controller.friendId] ==
                          null
                      ? 0
                      : controller.messageService
                          .messageMap[controller.friendId]!.length,
                  itemBuilder: (context, index) => Message(
                      message:  controller.messageService
                          .messageMap[controller.friendId]![index]),
                  controller: controller.scrollController,
                ),
              )),
              ChatInputField()
            ],
          )
      ),
    );
  }
}
